package com.atguigui.leetcode;

/**
 * 795.区间子数组个数
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/11/24 8:43
 */
public class P795NumberOfSubarraysWithBoundedMaximum {
    public static void main(String[] args) {
        Solution solution = new P795NumberOfSubarraysWithBoundedMaximum().new Solution();
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int numSubarrayBoundedMax(int[] nums, int left, int right) {
            //last1:大于等于 left且小于等于 right
            //last2:大于 right
            int res = 0, last2 = -1, last1 = -1;

            for (int i = 0; i < nums.length; i++) {
                if (nums[i] >= left && nums[i] <= right) {
                    last1 = i;
                } else if (nums[i] > right) {
                    last2 = i;
                    last1 = -1;
                }
                if (last1 != -1) {
                    res += last1 - last2;
                }
            }

            return res;
        }
    }
}
